package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.GoodsTypeDao;
import com.atguigu.jxc.entity.GoodsType;
import com.atguigu.jxc.entity.Unit;
import com.atguigu.jxc.service.GoodsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class GoodsTypeServiceImp implements GoodsTypeService {
    @Autowired
    private GoodsTypeDao goodsTypeDao;
    @Autowired
    private GoodsDao goodsDao;
   // 获取 商品分类信息
    @Override
    public List loadGoodsType() {
        ArrayList<Map> list = new ArrayList<>();
        // 获取父节点的信息
        GoodsType parentGoodsType = goodsTypeDao.getGoodType(1);
        HashMap<String, Object> PMap = new HashMap<String, Object>();
        PMap.put("id",parentGoodsType.getGoodsTypeId());
        PMap.put("text",parentGoodsType.getGoodsTypeName());
        PMap.put("state",parentGoodsType.getGoodsTypeState()==1? "closed":"open");
        PMap.put("iconCls","goods-type");
        HashMap<String, Object> PAttrmap = new HashMap<>();
        PAttrmap.put("state",parentGoodsType.getGoodsTypeState());
        PMap.put("attributes",PAttrmap);
        // 获取一级子节点数据
        List<GoodsType> firstChildrenList= goodsTypeDao.getGoodTypeByPID(parentGoodsType.getGoodsTypeId());
        List<Map<String, Object>> firstChildrenArrayList = firstChildrenList.stream().map(firstChildren -> {
            Map<String, Object> firstChildrenMap = new HashMap<>();
            firstChildrenMap.put("id", firstChildren.getGoodsTypeId());
            firstChildrenMap.put("text", firstChildren.getGoodsTypeName());
            firstChildrenMap.put("state", firstChildren.getGoodsTypeState()==1?"closed" : "open");
            firstChildrenMap.put("iconCls", "goods-type");
            HashMap<String, Object> firstChildrenAttrMap = new HashMap<>();
            firstChildrenAttrMap.put("state", firstChildren.getGoodsTypeState());
            firstChildrenMap.put("attributes", firstChildrenAttrMap);
            List<GoodsType> secondChildrenList = goodsTypeDao.getGoodTypeByPID(firstChildren.getGoodsTypeId());
            //判断 ，如果 有 二级子节点数据，那么 进行添加，如果没有，那么 就不进行添加
            if (!CollectionUtils.isEmpty(secondChildrenList)) {
                List<Object> secondChildrenArrayList = secondChildrenList.stream().map(secondChildren -> {
                    Map<String, Object> secondChildrenMap = new HashMap<>();
                    secondChildrenMap.put("id", secondChildren.getGoodsTypeId());
                    secondChildrenMap.put("text", secondChildren.getGoodsTypeName());
                    secondChildrenMap.put("state", secondChildren.getGoodsTypeState() == 1 ? "closed" : "open");
                    secondChildrenMap.put("iconCls", "goods-type");
                    HashMap<String, Object> secondChildrenAttrMap = new HashMap<>();
                    secondChildrenAttrMap.put("state", secondChildren.getGoodsTypeState());
                    secondChildrenMap.put("attributes", secondChildrenAttrMap);
                    return secondChildrenMap;
                }).collect(Collectors.toList());
                firstChildrenMap.put("children", secondChildrenArrayList);
            }
            return firstChildrenMap;
        }).collect(Collectors.toList());
        PMap.put("children",firstChildrenArrayList);
        list.add(PMap);
        System.out.println(PMap);
      return   list;
    }
    @Override
    public Map<String, Object> getUnitList() {
        HashMap<String, Object> map = new HashMap<>();
        List<Unit> unitList =  goodsDao.getUnitList();
       map.put("rows",unitList);
       return map;
    }
   // 保存商品分类
    @Override
    public void save(String goodsTypeName, Integer pId) {
        goodsTypeDao.save(goodsTypeName,pId);
    }
  // 删除商品分类
    @Override
    public void delete(Integer goodsTypeId) {
        goodsTypeDao.delete(goodsTypeId);
    }
}
